﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphGeneric;
using System.Drawing;

namespace GraphGenericTestApp
{
	public class GraphOld : IGraphOld<string, string>
	{

		private Dictionary<string, INodeOld<string, string>> m_dictVertices = new Dictionary<string, INodeOld<string, string>>();
		private Dictionary<string, List<IEdgeOld<string, string>>> m_dictEdges = new Dictionary<string, List<IEdgeOld<string,string>>>();

		#region IGraph<string,string> Members

		public IEnumerable<INodeOld<string, string>> AllVertices
		{
			get { return m_dictVertices.Values; }
		}

		public INodeOld<string, string> GetVertex(string value)
		{
			return m_dictVertices[value];
		}

		public IEnumerable<IEdgeOld<string, string>> GetEdges(string value)
		{
			return m_dictEdges[value];
		}

		public void AddVertex(string value)
		{
			if (!m_dictVertices.ContainsKey(value))
			{
				m_dictVertices[value] = new VertexOld(value);
				m_dictEdges[value] = new List<IEdgeOld<string, string>>();
			}
		}

		public void AddEdge(string from, string to, string weight, bool isDirected)
		{
			INodeOld<string, string> vFrom;
			INodeOld<string, string> vTo;
			if (m_dictVertices.TryGetValue(from, out vFrom) && m_dictVertices.TryGetValue(to, out vTo))
			{
				if (vFrom.AddAdjacentNode(vTo))
					m_dictEdges[from].Add(new EdgeOld(vFrom, vTo, weight, isDirected));
				if (!isDirected)
					if (vTo.AddAdjacentNode(vFrom))
						m_dictEdges[to].Add(new EdgeOld(vTo, vFrom, weight, isDirected));
			}
		}
		#endregion
	}
		
	public class VertexOld : INodeOld<string, string>
	{
		public VertexOld(string value)
		{
			m_value = value;
		}
		public override string ToString()
		{
			return Value;
		}
		private string m_value;
		private List<INodeOld<string, string>> m_adjacentVertices = new List<INodeOld<string, string>>();

		#region IVertex<string, string> Members

		public string Value
		{
			get { return m_value; }
		}

		public IEnumerable<INodeOld<string, string>> AdjacentNodes
		{
			get { return m_adjacentVertices; }
		}

		public bool AddAdjacentNode(INodeOld<string, string> vertex)
		{
			if (!m_adjacentVertices.Contains(vertex))
			{
				m_adjacentVertices.Add(vertex);
				return true;
			}
			return false;
		}

		#endregion
	}

	public class EdgeOld : IEdgeOld<string, string>
	{
		public EdgeOld(INodeOld<string, string> vertex1, INodeOld<string, string> vertex2, string weight, bool isDirected)
		{
			m_vertex1 = vertex1;
			m_vertex2 = vertex2;
			m_weight = weight;
			m_isDirected = isDirected;
		}
		public override string ToString()
		{
			return string.Format("{0} {1}--{2}--> {3}", m_vertex1, m_isDirected ? "-" : "<", m_weight, m_vertex2);
		}
		private INodeOld<string, string> m_vertex1;
		private INodeOld<string, string> m_vertex2;
		private string m_weight;
		private bool m_isDirected;

		#region IEdge<string,string> Members

		public INodeOld<string, string> Vertex1
		{
			get { return m_vertex1; }
		}

		public INodeOld<string, string> Vertex2
		{
			get { return m_vertex2; }
		}

		public string Weight
		{
			get { return m_weight; }
		}

		public bool IsDirected
		{
			get { return m_isDirected; }
		}

		#endregion
	}
}
